package com.sctvcloud.zhaohua_sd.ui.utils;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.ActionMenuView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.ijkplaylib.bean.IPlayClarity;
import com.ruihang.ijkplaylib.widget.GPlayerView;
import com.ruihang.ijkplaylib.widget.IGPlayerTopControll;
import com.sctvcloud.zhaohua_sd.R;
import com.sctvcloud.zhaohua_sd.beans.LiveInfo;

import java.lang.ref.WeakReference;

/**
 * Created by liuyu on 2017/8/8.
 */

public class TVTopControllerImp implements IGPlayerTopControll, View.OnClickListener {

    private WeakReference<AppCompatActivity> activityWeak;

    private CustomFontTextView title;
    private CustomFontTextView barrage;

    private View base;
    private View titleLay;
    private ImageView back;

    private boolean hasBarrage;

    private IBarrageControll barrageControll;
    private int barrageStateTemp = IBarrageControll.STATE_CLOSED;
    /**
     * 是否是直播
     */
    private boolean isLiving;

    public TVTopControllerImp(AppCompatActivity activity) {
        activityWeak = new WeakReference<AppCompatActivity>(activity);
    }

    @Override
    public void onAddingView(GPlayerView playerView, LayoutInflater inflater, ViewGroup group) {
        base = inflater.inflate(R.layout.living_play_top_controll, group, false);
        titleLay = base.findViewById(R.id.living_top_top_lay_title);
        back = (ImageView) base.findViewById(R.id.living_top_top_back);
        back.setOnClickListener(this);
        title = (CustomFontTextView) base.findViewById(R.id.living_top_top_title);
        barrage = (CustomFontTextView) base.findViewById(R.id.living_top_top_barrage);
        barrage.setOnClickListener(this);
        if (barrageStateTemp == IBarrageControll.STATE_OPENED) {
            barrage.setText(R.string.close_barrage);
        }
        barrage.setVisibility(hasBarrage ? View.VISIBLE : View.GONE);
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) base.getLayoutParams();
        lp.width = ActionMenuView.LayoutParams.MATCH_PARENT;
        group.addView(base, lp);
    }

    @Override
    public void onClarityChange(IPlayClarity clarity) {
    }

    @Override
    public void setHasBarrage(boolean hasBarrage) {
        this.hasBarrage = hasBarrage;
    }

    @Override
    public boolean onShowPanl(boolean isPortrait, boolean isLive, boolean isPlaying) {
        JLog.e("isPortrait=" + isPortrait + "  isLive=" + isLive + " isPlaying=" + isPlaying + "\n" + base);
        isLiving = isLive;
        if (base == null) {
            return false;
        }

        if (isPortrait) {
            base.setVisibility(View.VISIBLE);
            if (titleLay != null) {
                titleLay.setVisibility(View.VISIBLE);
            }
            if (back != null) {
                back.setVisibility(View.GONE);
            }
        } else {
            base.setVisibility(View.VISIBLE);
            if (titleLay != null) {
                titleLay.setVisibility(View.GONE);
            }
            if (back != null) {
                back.setVisibility(View.VISIBLE);
            }
        }
        return false;
    }

    @Override
    public void onHidePanl(boolean isPortrait, boolean isLive, boolean isPlaying) {
        if (base == null) {
            return;
        }
        isLiving = isLive;
        base.setVisibility(View.GONE);
    }

    @Override
    public void onPortrait() {
        JLog.e("base=" + base + " isLiving=" + isLiving);
        if (base == null) {
            return;
        }
        if (isLiving) {
            titleLay.setVisibility(View.GONE);
            back.setVisibility(View.GONE);
        } else {
            titleLay.setVisibility(View.VISIBLE);
            back.setVisibility(View.GONE);
        }
    }

    @Override
    public void onLandscape() {
        JLog.e("base=" + base + " isLiving=" + isLiving);
        if (base == null) {
            return;
        }
        titleLay.setVisibility(View.GONE);
        back.setVisibility(View.VISIBLE);
    }

    @Override
    public void onDetach(ViewGroup group) {
    }

    @Override
    public void onDestory() {
        barrageControll = null;
        if (barrage != null) {
            barrage.setOnClickListener(null);
        }
        if (back != null) {
            back = null;
        }
    }

    @Override
    public void onPlayerHide(GPlayerView playerView) {
    }

    @Override
    public void onClick(View v) {
        if (v == barrage) {
            if (barrageControll != null) {
                if (barrageStateTemp == IBarrageControll.STATE_CLOSED) {
                    barrageControll.openOrCloseBarrage(true);
                } else if (barrageStateTemp == IBarrageControll.STATE_OPENED) {
                    barrageControll.openOrCloseBarrage(false);
                }
            }
        } else if (v == back) {
            AppCompatActivity ac = getActivityTemp();
            if (ac != null) {
                ac.onBackPressed();
            }
        }
    }

    private AppCompatActivity getActivityTemp() {
        if (activityWeak != null) {
            return activityWeak.get();
        }
        return null;
    }

    public void setBarrageState(int state) {
        if (barrageStateTemp != state) {
            if (state == IBarrageControll.STATE_CLOSED) {
                barrageStateTemp = state;
                if (barrage != null) {
                    barrage.setText(R.string.show_barrage);
                }
            } else if (state == IBarrageControll.STATE_OPENED) {
                barrageStateTemp = state;
                if (barrage != null) {
                    barrage.setText(R.string.close_barrage);
                }
            }
        }
    }

    public void initInfo(LiveInfo info) {
        if (info == null) {
            return;
        }
        title.setText(info.getLiveTitle());
    }

    public void setTitle(String titleStr) {
        if (title != null) {
            title.setText(titleStr);
        }
    }

    public TVTopControllerImp setBarrageControll(IBarrageControll barrageControll) {
        this.barrageControll = barrageControll;
        return this;
    }
}
